<?php

/**
 * curses actions.
 *
 * @package    academia_de_escritores
 * @subpackage curses
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class coursesActions extends sfAeAction
{
  private function getDbUser(){
  	return $this->getUser()->getGuardUser()->getUser();
  }
 /**
  * Executes index action
  *
  * @param sfRequest $request A request object
  */
  public function executeIndex(sfWebRequest $request)
  {
   
  }
  
  public function executeShow(sfWebRequest $r){ 
    $Course= CourseTable::getInstance()->findOneBySlug($r->getParameter("slug")); 
    $this->forward404Unless($Course,"Page Not Found"); 
    $this->Course=$Course; 
    $this->PaimentType= SectionTable::getInstance()->findOneByCode("payment-format");
    $i18N=sfContext::getInstance()->getI18N();
    $routing=sfContext::getInstance()->getRouting();
    $this->getUser()->setFlash("navigation", array_merge(WebGlobal::getNavHome($i18N, $routing),
		    											   WebGlobal::getNavCourses($i18N, $routing),
		    											   array($this->Course->getName()=>array())
    											   )
    											   ,
    											   false);
    											   
  }
  public function executeListCourses(sfWebRequest $request){
  	
  	$q=CourseTable::getInstance()->getAvailableCourseQuery();
  	//$q->useResultCache(true,2*60,CourseTable::CACHE_LIST_PREFIX."index_pg");
  	
  	$i18N=sfContext::getInstance()->getI18N();
    $routing=sfContext::getInstance()->getRouting();
    $this->pager= new sfDoctrinePager("Course",8);
    $this->pager->setQuery($q);
    $this->pager->setPage($request->getParameter('pg', 1));
    $this->pager->init();
    $this->Courses=$this->pager->getResults();
  	 $this->getUser()->setFlash("navigation", array_merge(WebGlobal::getNavHome($i18N, $routing),
  	 											   WebGlobal::getNavCourses($i18N, $routing, true)
  	 												),false);
  }
  
  public function executeListCoursesHome(){
    
    $this->renderComponent("courses", "CoursesHome");
    return sfView::NONE;
  }
  public function executeShop(sfWebRequest $r){
      $this->token=$r->getParameter("token");
      $this->validateToken($this->token, true);
      $Publication=PublicationTable::getInstance()->findOneById($r->getParameter("publication_id"));
      $Course=CourseTable::getInstance()->findOneBySlug($r->getParameter("course_slug"));
      $this->forward404Unless($Publication,"Publication Not Found");
      $this->forward404Unless($Course,"Course Not Found");
      $this->forward404Unless($Course->getId()==$Publication->getCourseId(),"invalid Url");
      $this->Publication=$Publication;
      
  }
  
  public function executeConfirm(sfWebRequest $r){
      $this->token=$r->getParameter("token");
      $this->validateToken($this->token, true);
      $this->selected=2;
      if(!$this->getUser()->isAuthenticated()){
        return $this->shopSecure($r);
      }
      
      $PublicationPrice=PublicationPriceTable::getInstance()->findOneById($r->getParameter("pprice_id"));
      $Course=CourseTable::getInstance()->findOneBySlug($r->getParameter("course_slug"));
      $this->forward404Unless($PublicationPrice,"price not found");
      $this->forward404Unless($Course,"price not found");
      $this->forward404Unless($Course->getId()==$PublicationPrice->getPublication()->getCourseId(),"Invalid url");
      $Inscription= new Inscription();
      $Inscription->setUser($this->getDbUser());
      $Inscription->setPublicationPrice($PublicationPrice);
      $Inscription->setInscriptionStatus(InscriptionStatusTable::getInstance()->findOneByCode(InscriptionStatusTable::STATUS_APPROVED));
      $this->form=new InscriptionConfirmForm($Inscription);
      
      if($r->isMethod(sfWebRequest::PUT)){
          if($objIncr=$this->processForm($r, $this->form)){
              $this->removeToken($this->token);
          	  $this->getUser()->setFlash("shop_success",true);
              $this->redirect("shop_success",array("course_slug"=>$Course->getSlug(),"inscription_id"=>$objIncr->getId()));
          }
      }
      $this->Course=$Course;
      $this->Price=$PublicationPrice;
  }
  
  protected function  shopSecure(sfWebRequest $request){
    $this->setTemplate("shopSecure");
    return sfView::SUCCESS;
  }
  public function executeShopSuccess(sfWebRequest $request){
  	  $this->redirectUnless($this->getUser()->getFlash("shop_success",false),"@home");
      $ShopSuccess=SectionTable::getInstance()->findOneByCode(SectionTable::SECTION_SHOP_COURSE_SUCCESS);
      $this->title=$ShopSuccess->getTitle();
      $this->info=$ShopSuccess->getInfo();
      
  }
  
  public function executeGenerateToken(sfWebRequest $r){
    
    $token=dechex( rand(109,500) * rand(600,3000)*  time());
    $newToken[$token]=array("publication_id"=>$r->getParameter("publication_id"));
    $tokens=$this->getUser()->getAttribute("tokens",array(),"shop");
    $allTookens=array_merge($tokens,$newToken);
    $this->getUser()->setAttribute("tokens", $allTookens,"shop");
    $this->redirect("@shop_course?token={$token}&course_slug={$r->getParameter("course_slug")}&publication_id={$r->getParameter("publication_id")}");
  }
  protected function processForm(sfWebRequest $request, sfForm $form)
  {
    $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
    if ($form->isValid())
    {
      $obj = $form->save();
      return $obj; 
    }
    return false;
  }
  protected function validateToken($token,$forwad=false){
    $tokens=$this->getUser()->getAttribute("tokens",array(),"shop");
   if(isset($tokens[$token])){
     return $tokens[$token];
   }elseif($forwad){
     $this->forward404("Esta pagina ha expirado");
   }else{
     return false;
   }
   
  }
  protected function removeToken($token){
    $tokens=$this->getUser()->getAttribute("tokens",array(),"shop");
    unset($tokens[$token]);
    if(is_array($tokens) && count($tokens)>0){
      $this->getUser()->setAttribute("tokens", $tokens,"shop");
    }else{
      $this->getUser()->getAttributeHolder()->remove("tokens",true,"shop");
    }
    
  }
  
  
}
